﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using Qplus.protocol;
using Qplus.enums;
using Qplus.Infrastructure;
using System.Diagnostics;
using Qplus.beans;

namespace Qplus.UI
{
    public partial class ChatRoomControl : UserControl
    {
        chatroomCtrlTypeEnum type;
        int roomID;
        string roomName;
        int roomLevel;
        int userCount;
        int upperLimit;
        int roomHeadType;
        string roomHeadValue;
        int roomHeat;
        int roomPoint;
        int hotStar = 0;
        public ChatRoomControl()
        {
            InitializeComponent();
        }

        public chatroomCtrlTypeEnum CtrlType
        {
            set
            {
                switch (value)
                {
                    case chatroomCtrlTypeEnum.create:
                        {
                            type = value;
                            chatRoomNameTxt.Visibility = System.Windows.Visibility.Collapsed;
                            createTxt.Visibility = System.Windows.Visibility.Visible;
                            hotGrid.Visibility = System.Windows.Visibility.Collapsed;
                            ContentGrid.Background = new SolidColorBrush(new Color() { A = 0xDC, R = 0xFF, G = 0xFF, B = 0x00 });
                            ImageBrush bgBrh = new ImageBrush()
                            {
                                ImageSource = new BitmapImage(new Uri("/Qplus;component/Pic/icon_create_chatroom_item.png", UriKind.RelativeOrAbsolute)),
                                Stretch = Stretch.None,
                            };
                            chatRoomBtnEx.Background = bgBrh;
                            chatRoomBtnEx.PressedBackground = bgBrh;
                            chatRoomBtnEx.Click -= chatRoomBtnEx_Click;
                            break;
                        }
                    case chatroomCtrlTypeEnum.chatroom:
                        {
                            type = value;
                            //chatRoomBtnEx.Click += chatRoomBtnEx_Click;
                            break;
                        }
                    case chatroomCtrlTypeEnum.notype:
                    default:
                        {
                            break;
                        }
                }
            }
            get { return type; }
        }
        public int RoomID { get { return roomID; } set { roomID = value; } }
        public string RoomName { get { return roomName; } set { roomName = value; chatRoomNameTxt.Text = value; } }
        public int RoomLevel { get { return roomLevel; } set { roomLevel = value; } }
        public int UserCount { get { return userCount; } set { userCount = value; } }
        public int UpperLimit { get { return upperLimit; } set { upperLimit = value; } }
        public int RoomHeadType { get { return roomHeadType; } set { roomHeadType = value; } }
        public string RoomHeadValue { get { return roomHeadValue; } set { roomHeadValue = value; } }
        public int RoomHeat { get { return roomHeat; } set { roomHeat = value; } }
        public int RoomPoint { get { return roomPoint; } set { roomPoint = value; } }
        public SolidColorBrush bgColor
        {
            set { ContentGrid.Background = value; }
        }
        public ImageBrush bgImage
        {
            set
            {
                ContentGrid.Background = value;
            }
        }
        public int HotStar
        {
            set { hotStar = value; setHotStar(value); }
            get { return hotStar; }
        }
        public ImageBrush BtnImage
        {
            set
            {
                chatRoomBtnEx.Background = value;
                chatRoomBtnEx.PressedBackground = value;
            }
        }
        public void setHotStar( int hotNum)
        {
            /***********
            <Image  Grid.Column="1" Source="/Qplus;component/Pic/chatroom_hot.png" Stretch="None"/>
            <Image  Grid.Column="2" Source="/Qplus;component/Pic/chatroom_hot.png" Stretch="None"/>
            <Image  Grid.Column="3" Source="/Qplus;component/Pic/chatroom_hot.png" Stretch="None"/>
            <Image  Grid.Column="4" Source="/Qplus;component/Pic/chatroom_hot.png" Stretch="None"/>
            <Image  Grid.Column="5" Source="/Qplus;component/Pic/chatroom_hot.png" Stretch="None"/>
            *******************/
            Image[] hotImg = new Image[5];
            for (int i = 0; i < hotNum; i++)
            {
                hotImg[i] = new Image() 
                {
                    Source = new BitmapImage(new Uri("/Qplus;component/Pic/chatroom_hot.png", UriKind.RelativeOrAbsolute)),
                    Stretch = Stretch.None
                };
                Grid.SetColumn(hotImg[i],i+1);
                hotGrid.Children.Add(hotImg[i]);
            }
        }
        beans.QUser user = QplusT.Instance.getUser();
        private PacketQueue queue;
        private void chatRoomBtnEx_Click(object sender, RoutedEventArgs e)
        {
            switch (type)
            {
                case chatroomCtrlTypeEnum.chatroom:
                    {
                        gotoChatRoom(null, null);
                        ProtocolCommand.Instance.pcmd_C2U_REQ_ENTER_ROOM(RoomID);

                        //LoginHelper.Instance.enterRoomEvent += new EventHandler<UIEvent>(gotoChatRoom);
                        //UIData.Instance.GetChatList();
                        //(App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/UI/ChatRoom.xaml", UriKind.Relative));
                        break;
                    }
                case chatroomCtrlTypeEnum.create:
                    {
                        break;
                    }
                case chatroomCtrlTypeEnum.notype:
                default:
                    {
                        MessageBox.Show("@@" + "chatroom Control's Type is :" + type.ToString() + "!" + "@@");
                        break;
                    }
            }
        }

        void gotoChatRoom(object sender, UIEvent e)
        {
            //throw new NotImplementedException();
            Room.CurrentRoomInstance.RoomID = roomID;
            Room.CurrentRoomInstance.RoomName = roomName;
            //Dispatcher.BeginInvoke(() =>
            //{
                (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/UI/ChatRoom.xaml", UriKind.Relative));
            //});
        }
    }
}
